package com.blog.controller.manage;

import com.blog.bean.ArticleCategory;
import com.blog.constant.RoleConstant;
import com.blog.service.ArticleCategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/articleCategoryManage")
public class ArticleCategoryManageController {

    @Resource
    private ArticleCategoryService articleCategoryService;

    /**
     * 新增文章分类
     *
     * @param articleCategory
     * @return
     */
    @RequestMapping("/addArticleCategory")
    @RequiresRoles(RoleConstant.ROLE_ADMIN)
    @ResponseBody
    public Object addArticleCategory(ArticleCategory articleCategory) {
        Map<Object, Object> map = new HashMap<>();
        int count = articleCategoryService.insertSelective(articleCategory);
        if (count > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 获得文章主分类
     * 返回json
     *
     * @return
     */
    @RequestMapping("/selectArticleCategoryFirst")
    @RequiresRoles(RoleConstant.ROLE_ADMIN)
    @ResponseBody
    public Object selectArticleCategoryFirst() {
        Map<Object, Object> map = new HashMap<>();
        ArticleCategory articleCategory = new ArticleCategory();
        articleCategory.setArticleCategoryParentId((long) 0);
        List<ArticleCategory> articleCategoryList = articleCategoryService.selectArticleCategory(articleCategory);
        map.put("articleCategoryList", articleCategoryList);
        return map;
    }

    /**
     * 更新文章分类
     *
     * @param articleCategory
     * @return
     */
    @RequestMapping("/updateArticleCategory")
    @RequiresRoles(RoleConstant.ROLE_ADMIN)
    @ResponseBody
    public Object updateArticleCategory(ArticleCategory articleCategory) {
        Map<Object, Object> map = new HashMap<>();
        int count = articleCategoryService.updateByPrimaryKeySelective(articleCategory);
        if (count > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 获得文章分类详情
     *
     * @param articleCategoryId
     * @return
     */
    @RequestMapping("/getArticleCategory")
    @RequiresRoles(RoleConstant.ROLE_ADMIN)
    @ResponseBody
    public Object getArticleCategory(@RequestParam(value = "articleCategoryId") Long articleCategoryId) {
        Map<Object, Object> map = new HashMap<>();
        ArticleCategory ac = new ArticleCategory();
        ac.setArticleCategoryId(articleCategoryId);
        ArticleCategory articleCategory = articleCategoryService.getArticleCategory(ac);
        if (articleCategory != null) {
            map.put("articleCategory", articleCategory);
        }
        return map;
    }

    /**
     * 获得二级分类列表
     * 根据父分类，查子分类
     *
     * @param articleCategoryId
     * @param pn
     * @param model
     * @return
     */
    @RequestMapping("/articleCategorySecond")
    @RequiresRoles(RoleConstant.ROLE_ADMIN)
    public String articleCategorySecond(@RequestParam(value = "articleCategoryId") Long articleCategoryId,
                                        @RequestParam(value = "pn", defaultValue = "1") String pn,
                                        Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 15);
        ArticleCategory ac = new ArticleCategory();
        ac.setArticleCategoryParentId(articleCategoryId);
        List<ArticleCategory> articleCategoryList = articleCategoryService.selectArticleCategory(ac);
        PageInfo<ArticleCategory> pageInfo = new PageInfo<>(articleCategoryList);
        model.addAttribute("pageInfo", pageInfo);
        return "manage-article-category";
    }

    /**
     * 一级分类列表
     *
     * @param pn
     * @param model
     * @return
     */
    @RequestMapping("/articleCategoryListByFirst")
    @RequiresRoles(RoleConstant.ROLE_ADMIN)
    public String articleCategoryListByFirst(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 15);
        ArticleCategory ac = new ArticleCategory();
        ac.setArticleCategoryParentId((long) 0);
        List<ArticleCategory> articleCategoryList = articleCategoryService.selectArticleCategory(ac);
        PageInfo<ArticleCategory> pageInfo = new PageInfo<>(articleCategoryList);
        model.addAttribute("pageInfo", pageInfo);
        return "manage-article-category";
    }
}
